// pages/Old/Old.js
const App = getApp();
const uid = App.globalData.uid;
const server = App.path.Server


Page({

    /**
     * 页面的初始数据
     */
    data: {
        search: false,
        order: '',
        selectedData: { // 两个选项结果
            community: {
                uid: '',
                name: '',
                headImage: '',
                createTime: '',
                updateTime: '',
                delFlag: ''
            }, // 选择社区
            status: {
                name: '',
                uid: ''
            } // 选择状态
        },
        selectorDefaultData: {
            community: { // 选择社区，选项
                array: [],// 选项数组
                title: '选择社区', // 默认显示,仅显示
                width: 402 // 默认宽度
            },
            status: { // 选择状态，选项
                array: [{name: '全部', uid: ''},{name: '已到访', uid: 1},{name: '未到访', uid: 2}],// 选项数组
                title: '选择状态',// 默认显示,仅显示
                width: 208 // 默认宽度
            }
        },
        manData: [ // 老人数组
            
        ],
        userInfo: {},
        page: 1,
        unload: false,
        nameOrId: '',
        needResetManData: false,
        isTapSearch: false,
    },
    showLoading() {
        wx.showLoading({
            title: '',
            mask: true
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData(options)
        if (this.data.search) {
            // console.log(JSON.parse(this.data.order))
            // console.log(this.data)
            let obj = JSON.parse(this.data.order)
            let temp = this.data.selectedData
            temp.community.uid = obj.community || ''
            temp.status.uid = obj.status || ''
            this.setData({
                nameOrId: obj.idOrName,
                selectedData: temp,
                isTapSearch: true
            })
        }
        this.getCommunityList()
        this.getOldList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {
        wx.hideLoading()
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        console.log('onPullDownRefresh')
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        console.log('onReachBottom')
        if(!this.data.unload) {
            this.getOldList()
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    },

    // input 绑定内容
    setNameOrId(e){
        // console.log(e)
        this.setData({
            nameOrId: e.detail.value
        })
    },

    /***
     * 组件Selector回调函数 
     */
    _setSelectedData(e) {
        // console.log(e)
        let { key, value } = e.detail;
        switch (key) {
            case 'community':
                this.setData({
                    selectedData: Object.assign(this.data.selectedData, { community: value })
                })
                break;
            case 'status':
                this.setData({
                    selectedData: Object.assign(this.data.selectedData, { status: value })
                })
                break;
            default:
                break;
        }
    },

    // 扫描二维码
    scanCode() {
        App.scanCode(this)
    },

    // 获取社区名
    getCommunityList(){
        let _this = this
        wx.request({
            url: server + '/v1/community/getAllCommunity',
            method: 'POST',
            header: {
              'Content-Type': 'application/x-www-form-urlencoded',
              'Authorization': 'bearer ' + App.globalData.token.access_token
            },
            complete(res){
                App.judgeCode(res,function(_res){
                    let temp = _this.data.selectorDefaultData
                    temp.community.array = res.data
                    _this.setData({
                        selectorDefaultData: temp
                    })
                })
            }
        })
    },

    // 获取列表
    getOldList (data = {
        current: this.data.page,
        oldmanname: '',
        communityId: '',
        hasOrder: '',
        socialWorkId: this.data.userInfo.id
    }) {
        // console.log(this.data.userInfo)
        const _this = this
        if (this.data.isTapSearch) {
            data = {
                current: this.data.page,
                oldmanname: this.data.nameOrId || '',
                communityId: this.data.selectedData.community.uid || '',
                hasOrder: this.data.selectedData.status.uid || ''
            }
        }
        wx.request({
            url: server + '/v1/oldman/list',
            method: 'POST',
            header: {
              "Content-Type": "application/x-www-form-urlencoded",
              'Authorization': 'bearer ' + App.globalData.token.access_token
            },
            data: data,
            complete(res) {
                App.judgeCode(res,function(_res){
                    if(_res.data.current * _res.data.size >= _res.data.total) {
                        _this.setData({
                            unload: true
                        })
                    }
                    // console.log(_res.data)
                    let temp 
                    if(_this.data.needResetManData) {
                        temp = []
                        _this.setData({
                            needResetManData: false
                        })
                    }
                    else{
                        temp = _this.data.manData
                    }
                    temp.push(..._res.data.records)
                    _this.setData({
                        manData: temp,
                        page: _this.data.page + 1
                    })
                })
            }
        })
    },

    // 点击搜索后，排序获取列表
    searchOrderList () {
        this.setData({
            isTapSearch: true,
            needResetManData: true,
            page: 1
        })
        this.getOldList()
    }
})